<?php
declare (strict_types = 1);

namespace app\login\controller;

use app\login\model\Report;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\facade\Cache;
use think\facade\View;

class Admin
{
    //登录页面
    public function form()
    {
        return View::fetch('index/login');
    }

//    手机验证码
    public function captcha()
    {
        $phone = input('phone');
//        dd($phone);
        $obj = new \app\login\model\Admin();
        $res = $obj->captcha($phone);
        if ($res)
        {
//            dd('成功');
            $captcha = rand(1000,9999);
            $statusStr = array(
                "0" => "短信发送成功",
                "-1" => "参数不全",
                "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
                "30" => "密码错误",
                "40" => "账号不存在",
                "41" => "余额不足",
                "42" => "帐户已过期",
                "43" => "IP地址限制",
                "50" => "内容含有敏感词"
            );
            $smsapi = "http://api.smsbao.com/";
            $user = "lilong666"; //短信平台帐号
            $pass = md5("180011"); //短信平台密码
            $content="$captcha";//要发送的短信内容
            $phone = "15737055669";//要发送短信的手机号码
//        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
//        $result =file_get_contents($sendurl) ;
//        echo $statusStr[$result];
            Cache::set('captcha', $content, 60);
            return json([
                'msg'=>"验证码",
                'data'=>$content,
                'code'=>200,
            ]);
        }
    }
//登录功能
    public function login()
    {
        $data = input();
        $obj = new \app\login\model\Admin();
        $list = $obj->loginUse($data['phone']);
        if ($list)
        {
            return View::fetch('index/report',compact('list'));
        }
    }
//    举报添加功能
    public function report()
    {
        $data = input();

        $name = rand(10000,99999);
        $file = request()->file('img');
        try {
            validate(['image'=>'fileSize:1024*2|fileExt:jpg,png,jpeg'])
                ->check($data);
        } catch (\think\exception\ValidateException $e) {
             dd($e->getMessage());
        }
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::putFile( 'topic', $file);
        $data['img']="./public/storage/topic/20211220/$name";
        $obj = new Report();
        $res = $obj->reportAdd($data);


        $key=input('img');
        $image = request()->file('img');
        //获取上传后的文件路径
        // 图片存储在本地的临时路经
        $filePath = $image->getRealPath();
        // 获取图片后缀
        $ext = $image->getOriginalExtension();
        // 上传到七牛后保存的新图片名
        $newImageName  =   substr(md5($image->getOriginalName()),0,6)
            .  rand(00000,99999) . '.'.$ext;
        $auth = new Auth('Vu1Nlarf9dGtY4A2_CqxZ4ETd0MPgAk11rySpnWJ','PM6-1dCVJS0vzKJUJeSwyNDG2cVzRuGtbi_-cnwe');
        // 要上传的空间位置
        $token = $auth->uploadToken('zhezhishi');

        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        list($ret , $err) = $uploadMgr->putFile($token , $newImageName , $filePath);
//        if($err !== null){
//            return null;
//        }else{
//            // 图片上传成功
//            $da['image']=$newImageName;
//            $da['text']=$key;
//            $da['date']=date("Y/m/d");
//            $n=控制器名称::create($da);
//            if ($n){
//                return '添加成功';
//            }
//        }

        if ($res)
        {
            return json([
                'code'=>200,
                'msg'=>'举报内容已提交，请等待人工审核',
                'data'=>'',
            ]);
        }
    }
}
